package A1_字符串;public class LC_151翻转字符串里的单词 {
    public static void main(String[] args){
        String s="  hello world  ";
        System.out.println(reverseWords(s));
    }
    public static String reverseWords(String s) {
        //"  hello world  "
        s=s.trim();//hello world
        char[] c=s.toCharArray();
        StringBuilder str=new StringBuilder();//存放新的字符串
        int j=s.length()-1;
        int i=j;
        while(i>=0){
            while(i>=0 && c[i]!=' ')
                i--;
            str.append(s.substring(i+1,j+1)+" ");
            while(i>0 && c[i]==' ')
                i--;
            j=i;
        }
        s=String.valueOf(str);
        s=s.trim();
        return s;
    }
}
